package com.xiaojie.mapper;

import com.xiaojie.pojo.Role;
import com.xiaojie.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.mybatis.caches.redis.RedisCache;

import java.util.List;

/**
 * @ClassName UserMapper
 * @Author jiege
 * @Date 2021/5/6 21:09
 **/
@CacheNamespace(implementation = RedisCache.class)
public interface UserMapper {

    //查询所有用户信息，同时查询出每个用户关联的订单信息
    @Select("SELECT * FROM user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "orders",column = "id" ,javaType = List.class,many = @Many(select = "com.xiaojie.mapper.OrdersMapper.findOneByUid"))
    })
    public List<User> findALl();

    //查询所有用户、同时查询每个用户关联的角色信息
    @Select("SELECT * FROM user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "roleList",column = "id",javaType = List.class,many = @Many(select = "com.xiaojie.mapper.RoleMapper.findRoleByUid"))
    })
    public List<User> finAllUSerAndRole();

    //添加用户
    @Insert("insert into user values (#{id},#{username},#{password},#{birthday})")
    public void addUser(User user);

    //更新用户
    @Insert("update user set username = #{username} where id = #{id}")
    public void updateUser(User user);

    //查询
    @Select("select * from user")
    public List<User> selectUser();

    //删除
    @Delete("delete from user where id = #{id}")
    public void deleteUser(Integer id);

    //根据id查询用户
    @Select("select * from user where id = #{id}")
    public User findUserById(Integer id);
}
